/**
 * TabVD is a Virtual Desktop Client/Server designed specifically for connecting
 * from Tablet like devices.
 *
 * Copyright (C) 2011 Subhabrata Ghosh
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * DateUtils.java
 *
 *  Created on: Jul 19, 2011
 *      Author: subhagho
 */
package net.langene.tabvd.core;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author subhagho
 * 
 */
public class DateUtils {
	public static final String _DEFAULT_DATETIME_FORMAT_ = "MM-dd-yyyy HH:mm:ss";
	public static final String _DEFAULT_DATE_FORMAT_ = "MM-dd-yyyy";

	/**
	 * Get the current Date.
	 * 
	 * net.langene.tabvd.core.DateUtils
	 * 
	 * @return - Date.
	 */
	public static Date now() {
		Calendar cal = Calendar.getInstance();
		return cal.getTime();
	}

	/**
	 * Get the current date/time as string.
	 * 
	 * net.langene.tabvd.core.DateUtils
	 * 
	 * @param format
	 *            - Date/Time format.
	 * @return
	 */
	public static String nowstring(String format) {
		return datestring(now(), format);
	}

	/**
	 * Get the current date/time as string.
	 * 
	 * net.langene.tabvd.core.DateUtils
	 * 
	 * @return
	 */
	public static String nowstring() {
		return datestring(now());
	}

	/**
	 * Get the formatted date String, using the default format.
	 * 
	 * net.langene.tabvd.core.DateUtils
	 * 
	 * @param date
	 * @return
	 */
	public static String datestring(Date date) {
		SimpleDateFormat formatter = new SimpleDateFormat(
				_DEFAULT_DATETIME_FORMAT_);
		return formatter.format(date);
	}

	/**
	 * Get the formatted date String, using the specified format.
	 * 
	 * net.langene.tabvd.core.DateUtils
	 * 
	 * @param date
	 * @param format
	 * @return
	 */
	public static String datestring(Date date, String format) {
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		return formatter.format(date);
	}

	/**
	 * Parse the String as Date, using the default format.
	 * 
	 * net.langene.tabvd.core.DateUtils
	 * 
	 * @param date
	 * @return
	 * @throws Exception
	 */
	public static Date parsedate(String date) throws Exception {
		SimpleDateFormat formatter = new SimpleDateFormat(
				_DEFAULT_DATETIME_FORMAT_);
		return formatter.parse(date);
	}

	/**
	 * Parse the String as Date, using the specified format.
	 * 
	 * net.langene.tabvd.core.DateUtils
	 * 
	 * @param date
	 * @param format
	 * @return
	 * @throws Exception
	 */
	public static Date parsedate(String date, String format) throws Exception {
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		return formatter.parse(date);
	}
}
